package com.example.demo.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;

/**
 * @author wangsl
 * @date 2020/7/5 20:10
 * <p>
 * Description:
 */
@Configuration
public class AppConfig {

    @Bean(name = "myBean")//不配置name属性,默认使用方法名createBean获取bean
    @Scope("prototype")//默认使用单列singleton
    public MyBean createBean(){
        return new MyBean();
    }

    @Bean
    public RunnableFactoryBean createRunnableFactoryBean(){
        return new RunnableFactoryBean();
    }

    @Bean
    public CarFactory createCarFactory(){
        return new CarFactory();
    }

    @Bean
    public Car createCar(CarFactory carFactory){
        return carFactory.create();
    }

    @Bean
    public Cat createCat() {
        return new Cat();
    }

    @Bean
    public Bird createBird() {
        return new Bird();
    }

    @Bean
    public User createUser(){
        return new User();
    }

    @Bean//获取默认使用方法名
//    @Primary//当注入了多个userDao且未指定使用哪一个bean时,首先会选择使用createUserDao Bean注入其他Bean的依赖
    public UserDao createUserDao() {
        return new UserDao();
    }
}
